package com.example.helloworld

import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    private lateinit var textView: TextView
    private lateinit var flagView: ImageView
    private lateinit var switchButton: Button
    private var currentLanguage = 0 // 0: 英语, 1: 中文, 2: 日语
    private val flags = intArrayOf(R.drawable.flag_usa, R.drawable.flag_china, R.drawable.flag_japan)
    private val helloTexts = arrayOf("Hello World!", "你好，世界！", "こんにちは、世界！")
    private val switchTexts = arrayOf("Switch Language", "切换语言", "言語を切り替える")

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // 创建主布局：垂直线性布局
        val mainLayout = LinearLayout(this).apply {
            orientation = LinearLayout.VERTICAL
            setPadding(16, 16, 16, 16)
            layoutParams = LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT
            )
            gravity=android.view.Gravity.CENTER
        }

        // 创建国旗
        flagView = ImageView(this).apply {
            layoutParams = LinearLayout.LayoutParams(1000, 800).apply {
                setMargins(0, 0, 0, 16)
            }
            scaleType= ImageView.ScaleType.FIT_CENTER
        }

        // 创建文本
        textView = TextView(this).apply {
            textSize = 24f
            layoutParams = LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT
            ).apply {
                setMargins(0, 16, 0, 16)
            }
        }

        // 创建按钮
        switchButton = Button(this).apply {
            layoutParams = LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT
            )
        }

        mainLayout.addView(flagView)
        mainLayout.addView(textView)
        mainLayout.addView(switchButton)

        setContentView(mainLayout)
        title = getString(R.string.app_name) // 设置标题

        // 初始化视图
        updateLanguage()

        // 按钮点击事件
        switchButton.setOnClickListener {
            currentLanguage = (currentLanguage + 1) % 3
            updateLanguage()
        }
    }

    private fun updateLanguage() {
        flagView.setImageResource(flags[currentLanguage])
        textView.text = helloTexts[currentLanguage]
        switchButton.text = switchTexts[currentLanguage]
    }
}